/*
 * @Author: daifen
 * @Date: 2020-10-28 14:31:58
 * @LastEditors: Please set LastEditors
 */
/* 
  mongoose.js ：建立数据库连接
 */
var mongoose = require('mongoose') // 引入 mongoose
var DB_URL = "mongodb://root:root@192.168.50.222:27019/newDBName"; // 本地数据库地址
// var DB_URL = "mongodb://myadmin:secret@127.0.0.1/admin"; // 本地数据库地址
/**
 * 连接
 */
mongoose.connect(DB_URL, { useNewUrlParser: true, useUnifiedTopology: true });

/**
  * 连接成功
  */
mongoose.connection.once('connected', function () {
  console.log('Mongoose connection open to ' + DB_URL);
});

/**
 * 连接异常
 */
mongoose.connection.on('error', function (err) {
  console.log('Mongoose connection error: ' + err);
});

/**
 * 连接断开
 */
mongoose.connection.on('disconnected', function () {
  console.log('Mongoose connection disconnected');
});

module.exports = mongoose;

// // var MongoClient = require('mongodb').MongoClient;
// // var url = "mongodb://mongoadmin:mongoadmin@192.168.0.222:27019";

// // MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
// //     if (err) throw err;
// //     var dbo = db.db("newDBName");
// //     dbo.collection("fileData"). find({}).toArray(function(err, result) { // 返回集合中所有数据
// //         if (err) throw err;
// //         console.log(result);
// //         db.close();
// //     });
// // });